package com.midea.ai.air.ui.device.product;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.ui.device.product.data.FunItemInfo;
import com.midea.ai.air.ui.device.product.view.FuncItemView;
import com.midea.ai.air.ui.device.product.view.TitleBarView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/5/4.
 * 除湿机主页面Fragment
 */
public class DehumBaseFragment extends BaseFragment implements View.OnClickListener{
    private LinearLayout mFuncView01, mFuncView02, mFuncView03, mFuncView04;

    private List<FunItemInfo> mFunInfos;  //功能按钮List

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dehum_main, container, false);
        initTitleBar(view);
        initData();
        initViews(view);
        return view;
    }

    private void initTitleBar(View root)
    {
        TitleBarView titleBarView = (TitleBarView)root.findViewById(R.id.device_title_bar);
        titleBarView.setBackgroundColor(getResources().getColor(R.color.air_device_dehum));
        titleBarView.setTitleText(R.string.air_device_dehum);
        titleBarView.setOnLeftButtonClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //退出当前页面
                getActivity().finish();
            }
        });

        titleBarView.setOnRightButtonClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //前往更多设置
            }
        });
    }

    private void initData() {
        mFunInfos = new ArrayList<>();
        mFunInfos.add(new FunItemInfo("开关", R.drawable.ac_control_power));
        mFunInfos.add(new FunItemInfo("模式", R.drawable.ac_control_mode));
        mFunInfos.add(new FunItemInfo("风速", R.drawable.ac_control_wind));
        mFunInfos.add(new FunItemInfo("摆风", R.drawable.selector_control_luffing));

        mFunInfos.add(new FunItemInfo("舒适", R.drawable.selector_control_luffing));
        mFunInfos.add(new FunItemInfo("负离子", R.drawable.selector_control_ion));
        mFunInfos.add(new FunItemInfo("定时关", R.drawable.selector_control_timer));
    }

    private void initViews(View view) {
        mFuncView01 = (LinearLayout) view.findViewById(R.id.device_main_func_layout1);
        mFuncView02 = (LinearLayout) view.findViewById(R.id.device_main_func_layout2);
        mFuncView03 = (LinearLayout) view.findViewById(R.id.device_main_func_layout3);
        mFuncView04 = (LinearLayout) view.findViewById(R.id.device_main_func_layout4);

        for(int i=0; i<mFunInfos.size(); i++) {
            FunItemInfo info = mFunInfos.get(i);
            FuncItemView mFuncItemView = new FuncItemView(getActivity());
            mFuncItemView.setText(info.getStrName());
            mFuncItemView.setBackGroundRes(info.getResId());
            mFuncItemView.setOnCheckChangeListener(this);
            mFuncItemView.btn.setTag(info.getStrName());

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            layoutParams.gravity = Gravity.LEFT;
            switch (i/4) {
                case 0:
                    mFuncView01.addView(mFuncItemView, layoutParams);
                    break;
                case 1:
                    mFuncView02.addView(mFuncItemView, layoutParams);
                    break;
                case 2:
                    mFuncView03.addView(mFuncItemView, layoutParams);
                    break;
                case 3:
                    mFuncView04.addView(mFuncItemView, layoutParams);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {

    }

}
